package com.fy.fyspace.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPoolConfig {

  @Bean
  public ThreadPoolTaskExecutor imageProcessThreadPool() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(5); // 核心线程数
    executor.setMaxPoolSize(10); // 最大线程数
    executor.setQueueCapacity(100); // 队列容量
    executor.setKeepAliveSeconds(60); // 线程空闲时间
    executor.setThreadNamePrefix("image-process-"); // 线程名前缀
    // 拒绝策略：调用者运行
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    executor.initialize();
    return executor;
  }

  /**
   * AI任务处理线程池
   */
  @Bean("aiTaskThreadPool")
  public ThreadPoolExecutor aiTaskThreadPool() {
    return new ThreadPoolExecutor(
            5,                      // 核心线程数
            10,                    // 最大线程数
            60L,                   // 空闲线程存活时间
            TimeUnit.SECONDS,      // 时间单位
            new LinkedBlockingQueue<>(100),  // 任务队列
            new ThreadPoolExecutor.CallerRunsPolicy()  // 拒绝策略
    );
  }
}